#include "../skywind3000_rbtree.h"

#include <assert.h>
#include <stddef.h>
#include <stdio.h>

struct student {
    struct rb_node rb_node;
    int id;
};

int compare(const void *lhs, const void *rhs) {
    const struct student *l = lhs;
    const struct student *r = rhs;
    return l->id - r->id;
}

int main() {
    struct rb_root rb_root;
    rb_root_init(&rb_root);
    
    struct student stu;
    stu.id = 1;
    rb_node_init(&stu.rb_node);
    
    struct student stu2;
    stu2.id = 2;
    rb_node_init(&stu2.rb_node);
    
    struct rb_node *ret = 0;
    rb_insert_node(&rb_root, &stu.rb_node, compare, ret);
    assert(ret == 0);
    
    rb_insert_node(&rb_root, &stu2.rb_node, compare, ret);
    assert(ret == 0);
    
    ret = rb_node_first(&rb_root);
    assert(ret);
    struct student *iter = rb_node_entry(ret, struct student, rb_node);
    printf("first : %d\n", iter->id);

    ret = rb_node_last(&rb_root);
    assert(ret);
    iter = rb_node_entry(ret, struct student, rb_node);
    printf("last : %d\n", iter->id);
    
    struct student find_stu;
    find_stu.id = 2;
    
    rb_find_node(&rb_root, &find_stu.rb_node, compare, ret);
    assert(ret);
    iter = rb_node_entry(ret, struct student, rb_node);
    printf("find value : %d\n", iter->id);
    
    ret = rb_node_next(ret);
    assert(ret);
    iter = rb_node_entry(ret, struct student, rb_node);
    printf("find value next : %d\n", iter->id); 
    
    ret = rb_node_prev(ret);
    assert(ret);
    iter = rb_node_entry(ret, struct student, rb_node);
    printf("find value prev : %d", iter->id);
}

